<?php
/**
 * Created by PhpStorm.
 * Date: 2016/7/21
 * Time: 17:51
 */

/**
 * 首先接收rwid
 * 通过线路id得到
 * 首发站 终点站
 * 双向首末班车时间
 * 站点有序列表 [站点名称，换乘线，出口处，厕所数，[公交列表]]
 * [公交列表] [公交名，起始站，终点站，起始时间，终点时间,[站点有序列表]]
 * [站点有序列表] [站点名称]
 */
include 'RwRecord.php';

$RootDir = $_SERVER['DOCUMENT_ROOT'];
include $RootDir.'/utils/mysql/MysqlUtils.php';

//接收线路id
$rwid=$_POST['rwid'];
//echo "aa";
//$rwid = "1";
//echo $rwid;
$conn = conn_mysql();
//////////////////////////////////////////////////////////////////
//通过线路id得到线路信息
$query = "select rw_id as rwid,rw_name as rwName,'' as startStationId,start_station as startStation,'' as endStationId,
  end_station as endStation,upgo_spr_st as startTime,upgo_spr_et as endTime,downgo_spr_st as reverseStartTime,downgo_spr_et as reverseEndTime
from rail_way_line where rw_id = '$rwid'";

$result = queryRecord($query,$conn);
//echo $result;

$rwRecord = new RwRecord;
//echo "aa";
//
while ($row=mysql_fetch_array($result, MYSQL_ASSOC))//��$row=mysql_fetch_assoc($result)�ȼ�
{
//    echo "aa1";
    $rwRecord->init($row);
    break;
}

//echo "aa";

//////////////////////////////////////////////////////////////////
//通过线路id得到站点信息
$query = "select station_id as stationId,station_name as stationName,line_num as changeLines,exit_num as exportNum,
concat('男:',toilet_man_num,'|女:',toilet_woman_num) as toiletNum
from rail_way_station where rw_id = '$rwid'";
$result_station = queryRecord($query,$conn);

//echo "query1=".$query;

//echo "aa";
$stationInfos = array();
//
while ($row_station=mysql_fetch_array($result_station))//��$row=mysql_fetch_assoc($result)�ȼ�
{
//    echo "aa1";
    $stationInfo = new StationInfo();
    $stationInfo->init($row_station);

//    echo "stationId=".$stationInfo->stationId."</br>";

    //////////////////////////////////////////////////////////////////
    //通过站点信息得到公交信息
    $query = "select bi.bus_id as busId,bus_name as busName,start_station as busStartStation,end_station as busEndStation,station_time as busStartTime,end_time as busEndTime
from bus_station_relation bsr JOIN bus_info bi ON (bsr.bus_id = bi.bus_id) WHERE bsr.station_id = '$stationInfo->stationId'";
//    echo "query2=".$query."</br>";
    $result_bus = queryRecord($query, $conn);
    $busInfos = array();
    //
    while ($row_bus = mysql_fetch_array($result_bus))//��$row=mysql_fetch_assoc($result)�ȼ�
    {
        $busInfoIds = $row_bus['busId'];
        $busInfos[] = new BusInfo();
        $busInfo = $busInfos[count($busInfos)-1];
        $busInfo->init($row_bus);

        //////////////////////////////////////////////////////////////////
        //通过公交id得到公交站点信息
        $query = "select bs_name as busStation from bus_station WHERE bus_id = '$busInfo->busId'";
        $result_bs = queryRecord($query, $conn);

        $busStations = array();
        $index = 1;
        //
        while ($row_bs = mysql_fetch_array($result_bs))//��$row=mysql_fetch_assoc($result)�ȼ�
        {
            $busStations[] = array("busStationNo"=>strval($index),"busStation"=>$row_bs['busStation']);
            $index++;
        }
        $busInfo->busStations = $busStations;
    }
    $stationInfo->busInfos = $busInfos;
    $stationInfos[] = $stationInfo;
}

$rwRecord->stationInfos = $stationInfos;

//���ؽ��
//echo "aa3";
echo json_encode($rwRecord);

//$rwRecord1 = new RwRecord;
//$rwRecord1->startTime="aa";
//echo json_encode($rwRecord1);
//
//$arr = Array('1'=>'one', '2'=>'two', '3'=>'three');
//echo json_encode($arr);

mysql_close($conn);